package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.LoginResult;
import com.example.lanouhn.firstapp.activity.utils.DialogUtil;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.LoginJson;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

//登录页面
public class LoginActivity extends AppCompatActivity {

    //注册
    private TextView tv_register;
    private ImageView login_back;
    //进度条
    private ProgressBar progressBar;

    //关于我们--跳转
    private TextView tv_aboutOur;
    //免责声明
    private TextView tv_disclaimer;
    //商务合作
    private TextView tv_cooperate;
    //帮助中心
    private TextView tv_help;
    //登录
    private TextView login;

    //分别定义用户名和密码
    private EditText userName;
    private EditText passWords;
    private String key;
    //LoginResult 实体类集合的变量
    private LoginResult loginResult;

    private boolean isImageView = true;
    private LinearLayout ll_mineCenter;
    private ImageView mineCenter;
    private ImageView gzh;
    private ImageView iv_share;
    private ImageView phone;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //影藏进度条
            progressBar.setVisibility(View.GONE);
            switch (msg.what) {
                case 0://登陆  跳转

                    Intent intent = new Intent();
                    setResult(100, intent);
                    //关闭当前页
                    finish();
                    break;
                case 1:
                    DialogUtil.showDialog(LoginActivity.this, "用户名或者密码输入有误,请重试！", false);
                    break;
                case 2:
                    DialogUtil.showDialog(LoginActivity.this, "服务器响应异常，请稍后重试！", false);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //找控件
        initView();
        //点击事件
        setOnclickListener();

    }


    /**
     * 开启网络线程
     */
    private void getDataFormWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取登陆结果
                loginResult = loginPro();

            }
        }).start();
    }

    private LoginResult loginPro() {
        //获取用户名和密码
        String usename = userName.getText().toString();
        String paswords = passWords.getText().toString();

        loginResult = new LoginResult();


        //定义发送请求的url
        String url = "http://www.niaolema.com/mobile/index.php?act=login";
        String params = "username=" + usename + "&password=" + paswords + "&client=wap";
        String result = HttpUtils.doPost(url, params);

        if (null != result && !result.equals("")) {
            loginResult = LoginJson.getLogin(result);
            //解析出对应的登录标识  key值  然后通过Intent进行传递
            key = loginResult.getKey();


            if (null!=loginResult.getKey()) {

                //利用sharedpreforence类将数据保存在本地
                SPUtils.put(LoginActivity.this, "log_key", key);
                handler.sendEmptyMessage(0);
            } else {
                handler.sendEmptyMessage(1);
            }
        } else {
            //提示用户服务器异常
            handler.sendEmptyMessage(2);

        }
        return loginResult;
    }

    /**
     * 判断用户名  和密码输入框是否为空
     * DialogUtil.showDialog为自己封装好的工具类  用来弹出对话框信息
     *
     * @return
     */
    private boolean validate() {
        String usename = userName.getText().toString();
        if (usename.equals("")) {//用户名为空
            DialogUtil.showDialog(LoginActivity.this, "用户账户是必填的", false);
            return false;
        }
        String paswords = passWords.getText().toString();
        if (paswords.equals("")) {//密码为空
            DialogUtil.showDialog(LoginActivity.this, "用户密码是必填的", false);
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 点击事件集合
     */
    private void setOnclickListener() {

        login_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                setResult(105);
                finish();
            }
        });

        //点击登录时间按钮
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (validate()) {//判断输入框是否为空  返回true表示输入框非空   然后开启线程进行下一步的解析
                    //显示进度条
                    progressBar.setVisibility(View.VISIBLE);
                    getDataFormWeb();
                }
            }
        });
        //注册--点击事件
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        gzh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(LoginActivity.this,MainActivity.class);
                it.putExtra("symbol",1);
                startActivity(it);
            }
        });
        iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(LoginActivity.this,MainActivity.class);
                it.putExtra("symbol",2);
                startActivity(it);
            }
        });
        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(LoginActivity.this,MainActivity.class);
                it.putExtra("symbol",3);
                startActivity(it);
            }
        });
        //隐藏标题栏
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isImageView){
                    ll_mineCenter.setVisibility(View.VISIBLE);
                }else {
                    ll_mineCenter.setVisibility(View.GONE);
                }
                isImageView = !isImageView;
            }
        });
    }

    private void initView() {
        tv_register = (TextView) findViewById(R.id.mine_register);
        //绑定控件
        tv_aboutOur = (TextView) findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) findViewById(R.id.mine_cooperate);
        tv_help = (TextView) findViewById(R.id.mine_help);

        login_back = (ImageView) findViewById(R.id.login_back);
        login = (TextView) findViewById(R.id.login);
        userName = (EditText) findViewById(R.id.username);
        passWords = (EditText) findViewById(R.id.password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        //菜单栏
        ll_mineCenter = (LinearLayout) findViewById(R.id.ll_mineCenter);
        gzh = (ImageView) findViewById(R.id.gzh);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        phone = (ImageView) findViewById(R.id.phone);
        mineCenter = (ImageView) findViewById(R.id.menu);
    }


}
